<? 
require("adm_header.php"); 
writeCommonAdminTable("Import Tool",1);

echo "<b>Import Tool</b><br />";
echo "This tool will help you convert options and selected user values into selected table. It is important that you have imported all your textfiles into the Options table from Extra fields section of the admin area!";

echo "<p><form method='post' action='import_fields.php'>";
echo "<input type='checkbox' name='dry' value='1' checked='checked'> Dry run (no import, only show sql to be performed)";
echo " <p><input type='submit' name='submit_check' value='Convert'>";
echo "</form>";
$lDryTest=1;



if (getParam("submit_check","") && $set_demo!=1)
{
	
	if (getParam("dry","")==1)
	{
		echo "<p>Only test run</p>"; 
	}
	function insert($aType,$aId,$aFieldNumber,$aSelectedValue,$aText)
	{
		global $opt_tbl,$sel_tbl;
		
		$aSelectedValue=trim($aSelectedValue);
		$aSelectedValue=strToDb($aSelectedValue);
		if ($aSelectedValue)
		{
			$lSql="select opt_id,opt_name from $opt_tbl where opt_name='$aSelectedValue'";
			$lCheck=q($lSql);
			$lRowNew=mysql_fetch_array($lCheck);
			
			$lNewOptionId=$lRowNew["opt_id"];
		}
		//if ($aType==1)
		
		if ($aType==1)
			$lAdId=$aId;
		else if ($aType==2)
			$lUserId=$aId;
		
		$aText=strToDb($aText);
		
		$lInsSql="insert into $sel_tbl (sel_option_id,sel_option_ad,sel_option_user,sel_option_field,sel_option_text) values(";
		$lInsSql.="'$lNewOptionId','$lAdId','$lUserId','$aFieldNumber','$aText')";
		
		if (getParam("dry","")==1)
		{
			
			if ($aType==1)
				echo "Ad with number $lAdId processing now. Field e_$aFieldNumber<br />";
			else if ($aType==2)
				echo "Member with number $lUserId processing now. Field e_$aFieldNumber<br />";
				
			
			if ($aText=="")
				echo "In the old data, user has set option with text <b>$aSelectedValue</b>. In the new system, this is the same as option nb. <b>$lNewOptionId</b> in database-options.<br />";
			else if ($aText)
				echo "Text inserted into db.<br />";
						
			echo $lInsSql  . "<br>";
			echo "<hr />";
		}
		else 
		{
			echo "<p>Did real run: ";
			echo $lInsSql  . "<br>";
			$lDoRes=q($lInsSql);
			
		}
		
		
	}
	
	
	function doImport($aTable,$aType)
	{
		global $opt_tbl,$sel_tbl,$q_tbl;
		$lSql="select * from $aTable";
		$lRes=q($lSql);
		
		$lArray=getExtraFields($lRes);
		//print_r($lArray);
		
		if ($lArray>0)
		{
			foreach ($lArray as $lItem)
			{
				//echo $lItem . "<br />";
				$lTmpArray=split("\_",$lItem);
				$lFieldNb=$lTmpArray[1];
				
				$lGetQuestion="select * from $q_tbl where q_field=" . $lFieldNb;
				$lResQ=q($lGetQuestion);
				$lQuestionRow=mysql_fetch_array($lResQ);
				
				$lQuestionType=$lQuestionRow["q_type"];
				
					if ($lQuestionType=="s" || $lQuestionType=="r" || $lQuestionType=="c")
					{
						
						 $lOptions = file("config/options/$lFieldNb" . "_eng.txt");
						 /*
						$handle=fopen("config/options/$lFieldNb" . "_eng.txt","r");
						
						
					
						if ($handle) 
						{
		    				while (!feof($handle)) 
		    				{
		        				$buffer = fgets($handle, 4096);
		        				$lOptions[]=$buffer;
		    				}
		    				fclose($handle);
						}
						*/

					}
				
				
				if ($aType==1)
				{
					$lSql2="select $lItem,ad_id from $aTable";
					$lRes2=q($lSql2);
				}
				else if ($aType==2)
				{
					$lSql2="select $lItem,user_id from $aTable";
					$lRes2=q($lSql2);
				}
				
				
					while ($lRow=mysql_fetch_array($lRes2))
					{
						if ($lRow["ad_id"]!= null)
							$lAdOrUserId=$lRow["ad_id"];
						else if ($lRow["user_id"] !=null)
							$lAdOrUserId=$lRow["user_id"];
						
						if ($lQuestionType=="s" || $lQuestionType=="r" || $lQuestionType=="c")
						{
							$lSelected=$lRow[$lItem];
							
							
							if (eregi(",",$lSelected))
								$lSelected=split(",",$lSelected);
							
							if (is_array($lSelected))
							{
								foreach ($lSelected as $lSelItem)
								{
									if (trim($lOptions[$lSelItem]!=""))
										insert($aType,$lAdOrUserId,$lFieldNb,$lOptions[$lSelItem],"");			
								}
								
							}
							else 
							{
								if (trim($lSelected)!="")
									insert($aType,$lAdOrUserId,$lFieldNb,$lOptions[$lSelected],"");
							}
						
							
						}
						else 
						{
							$lSelected=$lRow[$lItem];
							
							if (trim($lSelected)!="")
								insert($aType,$lAdOrUserId,$lFieldNb,"",$lSelected);
						}
						/*
						if (!is_array($lSelected))
						{
							
							$lAdId=$lRow["ad_id"];
							$lOptionValue=trim($lOptions[$lSelected]);
							
							$lSql="select opt_id,opt_name from $opt_tbl where opt_name='$lOptionValue'";
							$lCheck=q($lSql);
							$lRowNew=mysql_fetch_array($lCheck);
							
							$lNewOptionId=$lRowNew=$lRowNew["opt_id"];
							
							$lInsSql="insert into $sel_tbl (sel_option_id,sel_option_ad,sel_option_field,sel_option_text) values(";
							$lInsSql.="$lNewOptionId,$lAdId,'')";
							echo $lInsSql;
						}
						*/
					}
					
					
				
				
				
			}
		}
		
		
	}
	echo "<p><b>ADS:</b></p>";
	doImport($ads_tbl,1);
	
	echo "<p><b>USER:</b></p>";
	doImport($usr_tbl,2);
	
	
}	
writeCommonAdminTable("Import Tool","");
require("adm_footer.php"); 
?>